﻿@model IniTechCaash.WebApplication.Models.JournalViewModel
@using IniTechCaash.Domain.Entities

@{
    ViewBag.Title = "General Ledger";
}

<h2>General Ledger</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                        Filter Journal Entries
                    </a>
                </h4>
            </div>

            <div id="collapseOne" class="panel-collapse collapse">
                <div class="panel">
                    <div class="form-horizontal">
                        @Html.ValidationSummary(true)

                        <div class="form-group">
                            @Html.LabelFor(model => model.StartDate, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.StartDate)
                                @Html.ValidationMessageFor(model => model.StartDate)
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.EndDate)
                                @Html.ValidationMessageFor(model => model.EndDate)
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.ShowPosted, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.ShowPosted)
                                @Html.ValidationMessageFor(model => model.ShowPosted)
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.ShowNonPosted, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.ShowNonPosted)
                                @Html.ValidationMessageFor(model => model.ShowNonPosted)
                            </div>
                        </div>
                    
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Submit" class="btn btn-default" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

<h4>Journal Entries</h4>
<p>
    @Html.ActionLink("Create New", "Create", null, new { @class="btn-default btn" })
</p>

@if (Model.JournalEntries != null && Model.JournalEntries.Count() > 0)
{
    <table class="table">
        <tr>
            <th>Account</th>
            <th class="text-right">DR</th>
            <th class="text-right">CR</th>
            <th>Description</th>
        </tr>
        @foreach (var item in Model.JournalEntries) {
            <tr>
                <td colspan="5"><strong>@Html.ActionLink(item.CompositeId, "Details", new { id=item.Id })</strong></td>
                @if (item.IsPosted)
                {
                    <td>Posted</td>
                }
                else if (User.IsInRole("Manager"))
                {
                    <td>
                        <p>
                        @Html.ActionLink("Post", "Post", new { id = item.Id }) |
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                        </p>
                    </td>
                }
                else if (item.CreatedBy == User.Identity.Name)
                {
                    <td>
                        <p>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                        </p>
                    </td>
                }
                else
                {
                    <td><p>@Html.ActionLink("Delete", "Delete", new { id = item.Id })</p></td>
                }
            </tr>
            foreach (var line in item.Lines.OrderByDescending(l => l.Side))
            {
                <tr>
                    @if (line.Side == NormalSide.Debit)
                    {
                        <td>
                            &nbsp;&nbsp;@Html.ActionLink(line.Account.Name, "Details", "FinanceAccount", new { id = line.Account.Id }, null)
                        </td>
                        <td class="text-right">
                            @Html.DisplayFor(l => line.Amount)
                        </td>
                        <td class="text-right"></td>
                    }
                    else
                    {
                        <td class="credit-indent">
                            &nbsp;&nbsp;@Html.ActionLink(line.Account.Name, "Details", "FinanceAccount", new { id = line.Account.Id }, new { @class="credit-indent" })
                        </td>
                        <td class="text-right"></td>
                        <td class="text-right">
                            @Html.DisplayFor(l => line.Amount)
                        </td>
                    }
                    <td>
                        @Html.DisplayFor(l => line.Description)
                    </td>
                </tr>
            }
        }
    </table>
}
else
{
    <p>No Journal Entries</p>
}